﻿using System;
using System.Collections.Generic;
using BLToolkit.Data;
using BLToolkit.DataAccess;
using PMIS.BLL.System;

namespace PMIS.BLL.Post
{
	/// <summary>
	/// 岗位_分类_等级
	/// </summary>
	public class BL_岗位_分类_等级 : BL_Common<岗位_分类_等级>
	{
		/// <summary>
		/// 保存
		/// </summary>
		/// <param name="kind">分类</param>
		/// <param name="items">记录</param>
		/// <returns></returns>
		public bool Save(string kind, List<岗位_分类_等级> items)
		{
			if (items == null || items.Count == 0)
				return false;

			bool result = false;
			DbManager dm = null;
			try
			{
				//	创建数据库连接
				dm = BL_DatabaseConnection.CreateConnection();
				//	启动事务
				dm.BeginTransaction();

				//	删除原有等级
				dm.SetCommand("Delete From 岗位_分类_等级 Where 分类 = @KIND",
					dm.Parameter("KIND", kind))
					.ExecuteNonQuery();

				int index = 1;
				SqlQuery<岗位_分类_等级> sq = new SqlQuery<岗位_分类_等级>(dm);
				foreach (岗位_分类_等级 item in items)
				{
					//	分类
					item.分类 = kind;
					//	排序
					item.排序 = index++;
					//	新增等级
					if (sq.Insert(item) == 0)
						throw new Exception("保存岗位_分类_等级时发生错误!");
				}

				//	提交事务
				dm.CommitTransaction();
				result = true;
			}
			catch (Exception ex)
			{
				if (dm != null)
					dm.RollbackTransaction();
				BL_ExceptionOperation.WriteException(ex, dm);
				result = false;
			}
			finally
			{
				if (dm != null)
				{
					dm.Close();
					dm.Dispose();
				}
			}

			return result;
		}
		/// <summary>
		/// 获取指定分类的等级
		/// </summary>
		/// <param name="kind">分类</param>
		/// <returns></returns>
		public List<岗位_分类_等级> GetDetails(string kind)
		{
			if (string.IsNullOrEmpty(kind))
				return null;

			List<岗位_分类_等级> result = null;
			DbManager dm = null;
			try
			{
				//	创建数据库连接
				dm = BL_DatabaseConnection.CreateConnection();

				//	查询
				result = dm.SetCommand("Select U.* From 岗位_分类_等级 U Where U.分类 = @KIND Order By 排序 Asc",
					dm.Parameter("KIND", kind))
					.ExecuteList<岗位_分类_等级>();
			}
			catch (Exception ex)
			{
				BL_ExceptionOperation.WriteException(ex);
				result = null;
			}
			finally
			{
				if (dm != null)
				{
					dm.Close();
					dm.Dispose();
				}
			}

			return result;
		}
	}
}
